package server2;


import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;

public class MyClassLoader extends ClassLoader {
  
    /** 
     * name class 类的绝对路径
     */
    protected Class<?> findClass(String basePath,String name)  {
        System.out.println("basePath:"+basePath);
        System.out.println("name:"+name);
        String myPath = basePath +  name.replaceAll("\\.", Matcher.quoteReplacement(File.separator)) + ".class";
        System.out.println("myPath:"+myPath);
        byte[] cLassBytes = null;
        Path path = null;

            path = Paths.get(myPath);
        try {
            cLassBytes = Files.readAllBytes(path);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Class clazz = defineClass(name, cLassBytes, 0, cLassBytes.length);
        return clazz;
    }

   public static void main(String[] args) {
       Class<?> aClass = new MyClassLoader().findClass("G:\\github以前做的作业\\stage2_module1_minicat-master\\webapps\\demo1\\",
               "server.LagouServlet");
       System.out.println("aClass:"+aClass);
   }


}  